﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AUV5.Web.Testing
{
    public partial class AddVideo : PageBaseFrontend
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["serviceID"] != null)
                {
                    string serviceID = Request["serviceID"].ToString();
                    initService(serviceID);
                    bindAttach(serviceID);
                    bindUserRelated(serviceID);
                    bindKeyRelated(serviceID); 
                }
            }
        }

        private void initService(string serviceID)
        {
            Model.ServiceInfo service = BLL.ServiceInfoBLL.Instance.GetDetail(serviceID);
            title.Text=service.Title;
            tags.Text=BLL.ServiceInfoBLL.Instance.GetTagList(serviceID);
            addby.Text=service.UserName;
        }

       /// <summary>
       /// 绑定该用户的其它信息
       /// </summary>
        private void bindUserRelated(string serviceID)
        {
            string userID = BLL.ServiceInfoBLL.Instance.GetDetail(serviceID).UserID;
            Model.NetPager pager = new Model.NetPager();
            pager.PageIndex = 1;
            pager.TableName = "ServiceInfo";
            pager.PrimaryKey = "ServiceID";
            pager.Where = "statusID>3 and UserID='"+userID+"' and ServiceID!='"+serviceID+"'";
            pager.Order = "UpdateDate Desc";
            pager.SortType = (int)Common.Enum.SortType.Multi;
            pager.PageSize = 10;
            pager.RecorderCount = 10;
            IList<AUV5.Model.ServiceInfo> list = AUV5.BLL.ServiceInfoBLL.Instance.GetServiceInfoListPage(pager);
            repUserRelated.DataSource = list;
            repUserRelated.DataBind();
        }
        /// <summary>
        /// 绑定该信息的相关信息
        /// </summary>
        private void bindKeyRelated(string serviceID)
        {
            //获取所有关键字列表
            string keywords = BLL.ServiceInfoBLL.Instance.GetTagList(serviceID);
            if (keywords != "")
            {
                //获取与该关键字相关的信息ID列表
                string idlist = BLL.ServiceKeyRelaBLL.Instance.GetListByKeyList(keywords.Split(','),serviceID,10);
                //根据ID列表获取所有信息
                if (idlist != "")
                {
                    Model.NetPager pager = new Model.NetPager();
                    pager.PageIndex = 1;
                    pager.TableName = "ServiceInfo";
                    pager.PrimaryKey = "ServiceID";
                    pager.Where = "ServiceID IN (" + idlist + ")";
                    pager.Order = "UpdateDate Desc";
                    pager.SortType = (int)Common.Enum.SortType.Desc;
                    pager.PageSize = 10;
                    pager.RecorderCount = 10;
                    repKeyRelated.DataSource = BLL.ServiceInfoBLL.Instance.GetServiceInfoListPage(pager);
                    repKeyRelated.DataBind();
                }
            }
        }
        //绑定附件
        private void bindAttach(string serviceID)
        {
            repAttach.DataSource = BLL.ServiceAttachmentBLL.Instance.GetListByServiceID(4, serviceID);
            repAttach.DataBind();
        }
        //添加视频
        protected void addVideo_Click(object sender, EventArgs e)
        {
            if (Request["serviceID"] != null)
            {
                string serviceID = Request["serviceID"].ToString();
                Model.ServiceAttachment att = new Model.ServiceAttachment();
                att.ServiceID = serviceID;
                string videopath = hfpath.Value;
                string sourceFile = Server.MapPath(videopath);
                string destpath = "/PlayFile/" + DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Day.ToString() + "/";
                if (!System.IO.Directory.Exists(Server.MapPath(destpath)))
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath(destpath));
                }
                Common.FormatConverter converter = new Common.FormatConverter();
                string uniquename = Common.FileHelper.GetUniqueFileName();
                //转换MP4格式
                converter.Convert(sourceFile,destpath,uniquename, Common.FormatConverter.VideoType.MP4, false,false);
                //转换Flv格式
                if (converter.Convert(sourceFile, destpath,uniquename, Common.FormatConverter.VideoType.FLV, true,true) == "")
                {
                    if (converter.DestVideo != "") 
                    {
                        att.VideoPath = converter.DestVideo;
                    }
                    if (converter.DestImage != "")
                    {
                        att.ImagePath = converter.DestImage;
                    }
                    BLL.ServiceAttachmentBLL.Instance.Add(att);
                }
                bindAttach(serviceID);
            }
        }

        //移除视频
        protected void delVideo_Click(object sender, EventArgs e)
        {
            if (Request["serviceID"] != null)
            {
                string serviceID = Request["serviceID"].ToString();
                Model.ServiceAttachment att = BLL.ServiceAttachmentBLL.Instance.GetVideo(serviceID);
                if (att!=null)
                {
                    BLL.ServiceAttachmentBLL.Instance.Delete(att);
                }
                bindAttach(serviceID);
            } 
        }

        //通过视频的审核，使其能显示到服务信息中
        protected void passVideo_Click(object sender, EventArgs e)
        {
            if (Request["serviceID"] != null)
            {
                string serviceID = Request["serviceID"].ToString();
                Model.ServiceAttachment att = BLL.ServiceAttachmentBLL.Instance.GetVideo(serviceID);
                if (att != null&&att.State==0)
                {
                    att.State = 1;
                    BLL.ServiceAttachmentBLL.Instance.Update(att);
                }
                bindAttach(serviceID);
            }
        }
    }
}